package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.View;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.EditStaffGoalAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.GroupGoal;
import com.swz.dcrm.model.StaffGoal;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.ui.viewmodel.GroupViewModel;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class EditStaffGoalFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.tv_total)
    TextView tvTotal;

    private EditStaffGoalAdapter editStaffGoalAdapter;

    private GroupGoal group;

    @Inject
    GroupViewModel goalViewModel;

    public static EditStaffGoalFragment newInstance() {

        return new EditStaffGoalFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())) {
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_edit_staff_goal;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableLoadmore(false);
        group = new Gson().fromJson(EditStaffGoalFragmentArgs.fromBundle(getArguments()).getGroup(), GroupGoal.class);
        title.setText(getString(R.string.edit_staff_goal_title, group.getGroupName(), group.getMonth()));
        editStaffGoalAdapter = new EditStaffGoalAdapter(getContext(), group.getMemeberGoalVOList());
        editStaffGoalAdapter.setTextChangeListener(textChangeListener);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.setAdapter(editStaffGoalAdapter);
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }

    @OnClick(R.id.btn_save)
    public void   save(){

        goalViewModel.updateGroup(group)
                .observe(this, groupBaseResponse -> {
                    showToast(groupBaseResponse.getMsg());
                });
    }
    EditStaffGoalAdapter.TextChangeListener textChangeListener = (list) -> {
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        for (StaffGoal staffGoal : list) {
            count1 += staffGoal.getBuildFileCount();
            count2 += staffGoal.getOrderCarCount();
            count3 += staffGoal.getDeliveryCarCount();
        }
        group.setMemeberGoalVOList(list);
        tvTotal.setText(getString(R.string.edit_staff_goal_total, count1, count2, count3));
    };
}
